package bd.configuration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class MySql {
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost/aspect_library";
	private static String user = "root";
	private static String pass = "";

	private static Connection connection;
	
	private MySql(){}
	
	/* To be able for everyone classes. */
	static{
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, pass);
			//System.out.println("done");
		} catch (ClassNotFoundException e) {
			System.out.println("Erro ao carregar o driver JDBC do MySQL.");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws SQLException{
		if (connection == null || connection.isClosed()){
			connection = DriverManager.getConnection(url, user, pass);
		}
		return connection;
	}
	


//	public void loadMySql() throws SQLException {
//		try {
//			Class.forName(driver);
//			connection = DriverManager.getConnection(url, user, pass);
//			//System.out.println("done");
//		} catch (ClassNotFoundException e) {
//			System.out.println("Erro ao carregar o driver JDBC do MySQL. ");
//		}
//	}

//	public static void createTransaction() throws SQLException{
//		getConnection();
//		connection.setAutoCommit(false);
//	}
//	public static Savepoint createSavePoint(String name) throws SQLException{
//		getConnection();
//		return connection.setSavepoint(name);
//	}
//	public static void commitTransaction() throws SQLException{
//		getConnection();
//		connection.commit();
//		connection.setAutoCommit(true);
//	}
//	public static void rollbackTransaction() throws SQLException{
//		getConnection();
//		connection.rollback();
//		connection.setAutoCommit(true);
//	}
//	public static void rollbackTransaction(Savepoint savepoint) throws SQLException{
//		getConnection();
//		connection.rollback(savepoint);
//		connection.setAutoCommit(true);
//	}
}
